import Vue from 'vue';//class
import axios from 'axios';
import store from './vuex.js'

//配置axios

// 1.配置实例
// axios = axios.create({
// 	baseURL: '/api/',
// 	timeout: 1000,
// 	headers: {'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFsZXgiLCJfaWQiOiI1ZWZkM2U3NmUzM2ViNDBmNWMyZmZhMDMiLCJpYXQiOjE2MzYwNzUxNjAsImV4cCI6MTYzNjE2MTU2MH0.VAyfbaVs4zljNshVm7IE86CbZXMTdxVO1X63QPYWFwo'}
// });


//2.全局配置
// axios.defaults.baseURL = '/api/';
// axios.defaults.headers.token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFsZXgiLCJfaWQiOiI1ZWZkM2U3NmUzM2ViNDBmNWMyZmZhMDMiLCJpYXQiOjE2MzYwNzUxNjAsImV4cCI6MTYzNjE2MTU2MH0.VAyfbaVs4zljNshVm7IE86CbZXMTdxVO1X63QPYWFwo'

//3.拦截器
// 响应拦截
axios.interceptors.request.use(function (config) {
    // 做一些事
	// config.headers.token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFsZXgiLCJfaWQiOiI1ZWZkM2U3NmUzM2ViNDBmNWMyZmZhMDMiLCJpYXQiOjE2MzYzNDM5NzcsImV4cCI6MTYzNjQzMDM3N30.jJxe45kyD2aGg7jPQkCIFrA6py8x4xnueuKUIwKkrPU'
	// let user = window.localStorage.getItem('user'); // {err:0,msg:'xxx',data:{},token:xxx}
	
	let token = store.state.user.user.token
    console.log('interceptors toekn',token);
	config.headers.token = token;
	config.baseURL = '/api/'
	
	//控制loding显示
	store.commit('bLoading',true)
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
	//控制loding隐藏
	store.commit('bLoading',false)
    return response.data;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  });






Vue.prototype.$axios = axios;

export default axios;